package com.zcs.app.wisdomschool;


import android.Manifest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.greateffect.littlebud.lib.base.BaseNormalActivityAdv;
import com.greateffect.littlebud.lib.cache.MMKVUtils;
import com.greateffect.littlebud.lib.dialog.CustomDialogHelper;
import com.greateffect.littlebud.lib.utilcode.FragmentUtils;
import com.greateffect.littlebud.lib.utils.FileCommonUtil;
import com.greateffect.littlebud.lib.utils.JColors;
import com.zcs.app.wisdomschool.ui.OneKeySubmitActivity_;
import com.zcs.app.wisdomschool.ui.fragment.HomeFragment_;
import com.zcs.app.wisdomschool.ui.fragment.MeFragment_;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.RuntimePermissions;

@EActivity(R.layout.activity_main)
@RuntimePermissions
public class MainActivity extends BaseNormalActivityAdv {
    private Fragment[] mFragments = new Fragment[2];
    private int curIndex = 0;

    @AfterViews
    void init() {
        // 初始化Fragment
        initFragments();
        onInit();
    }

    // ******************************* 申请权限1 *******************************
    @NeedsPermission({
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    })
    void onInit() {
        MMKVUtils.put("VersionCode", BuildConfig.VERSION_CODE);
        MMKVUtils.put("VersionName", BuildConfig.VERSION_NAME);
    }

    @OnPermissionDenied({
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    })
    void onDenied() {
        CustomDialogHelper.DialogParam dialogParam = new CustomDialogHelper.DialogParam();
        dialogParam.setTitle("授权被拒绝");
        dialogParam.setMessage(String.format("为保证%s的正常使用，请为%s授予%s权限。", mAppName, mAppName, "[存储]"));
        dialogParam.setPositiveBtnText("重新授权");
        dialogParam.setNegativeBtnText("退出");
        CustomDialogHelper.showCustomConfirmDialog(this, dialogParam, new CustomDialogHelper.NativeDialogCallback() {
            @Override
            public void onConfirm() {
                onInit();
            }

            @Override
            public void onCancel() {
                finish();
            }
        });
    }

    @OnNeverAskAgain({
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    })
    void onNeverAsk() {
        // 申请权限时被拒绝了，并且不再询问。
        CustomDialogHelper.DialogParam dialogParam = new CustomDialogHelper.DialogParam();
        dialogParam.setTitle("授权被拒绝");
        dialogParam.setMessage(getString(R.string.permission_tips_never_ask, mAppName, mAppName, "[存储]"));
        dialogParam.setPositiveBtnText("应用设置");
        dialogParam.setNegativeBtnText("退出");
        CustomDialogHelper.showCustomConfirmDialog(this, dialogParam, new CustomDialogHelper.NativeDialogCallback() {
            @Override
            public void onConfirm() {
                isGoToAppSettings = true;
                openAppDetailSettings();
            }

            @Override
            public void onCancel() {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isGoToAppSettings) {
            onInit();
            isGoToAppSettings = false;
        }
    }
    // ******************************* 申请权限2 *******************************


    private void initFragments() {
        mFragments[0] = new HomeFragment_();
        mFragments[1] = new MeFragment_();
        FragmentUtils.add(getSupportFragmentManager(), mFragments, R.id.id_index_frame_container, curIndex);
        // 设置初始被选中的tab
        if (curIndex == 0) change2HomeUI();
        else change2MeUI();
    }

    @ViewById(R.id.id_iv_tab_home)
    ImageView ivTabHome;
    @ViewById(R.id.id_iv_tab_me)
    ImageView ivTabMe;
    @ViewById(R.id.id_tv_tab_home)
    TextView tvTabHome;
    @ViewById(R.id.id_tv_tab_me)
    TextView tvTabMe;

    @Click({
            R.id.id_tab_home,
            R.id.id_tab_me,
            R.id.id_iv_tab_submit,
    })
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_iv_tab_submit:
                launchActivity(OneKeySubmitActivity_.class);
                break;
            case R.id.id_tab_home:// 首页
                changeFragment(0);
                break;
            case R.id.id_tab_me:// 我的
                changeFragment(1);
                break;
        }
    }

    private void changeFragment(int position) {
        curIndex = position;
        FragmentUtils.showHide(curIndex, mFragments);

        switch (position) {
            case 0:// 首页
                change2HomeUI();
                break;
            case 1:// 我的
                change2MeUI();
                break;
        }
    }

    private void change2HomeUI() {
        tvTabHome.setTextColor(JColors.colorPrimary);
        tvTabMe.setTextColor(0xFF595959);
//        ivTabHome.setSelected(true);
//        ivTabMe.setSelected(false);
        ivTabHome.setImageResource(R.mipmap.ic_tab_home_on);
        ivTabMe.setImageResource(R.mipmap.ic_tab_me);
    }

    private void change2MeUI() {
        tvTabMe.setTextColor(JColors.colorPrimary);
        tvTabHome.setTextColor(0xFF595959);
//        ivTabMe.setSelected(true);
//        ivTabHome.setSelected(false);
        ivTabHome.setImageResource(R.mipmap.ic_tab_home);
        ivTabMe.setImageResource(R.mipmap.ic_tab_me_on);
    }

    @Nullable
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            curIndex = savedInstanceState.getInt("curIndex");
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curIndex", curIndex);
    }
}
